package com.acxy.weixin.service;

import com.acxy.ants.common.kit.HttpKit;
import com.acxy.ants.core.annotation.Service;
import com.acxy.ants.core.annotation.Value;
import com.acxy.weixin.kit.AccessToken;
import com.acxy.weixin.kit.ApiResult;

/**
 * @author MrShun
 * @version 1.0
 * @Date 2017-11-04
 */
@Service
public class WxUserService {

    @Value("wx.appId")
    private String appId;

    @Value("wx.appSecret")
    private String appSecret;

    //获取用户基本信息 GET
    private final static String user_api = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN";

    //批量获取用户信息 POST
    private final static String batch_user_api = "https://api.weixin.qq.com/cgi-bin/user/info/batchget?access_token=ACCESS_TOKEN";

    //公众号给用户进行备注 POST
    private final static String update_remark_api = "https://api.weixin.qq.com/cgi-bin/user/info/updateremark?access_token=ACCESS_TOKEN";

    //拉取用户列表 GET
    private final static String user_list_api = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=ACCESS_TOKEN&next_openid=NEXT_OPENID";

    //拉取微信黑名单列表 POST
    private final static String black_user_list_api= "https://api.weixin.qq.com/cgi-bin/tags/members/getblacklist?access_token=ACCESS_TOKEN";

    //批量拉黑微信用户 POST
    private final static String batch_black_user_api= "https://api.weixin.qq.com/cgi-bin/tags/members/batchblacklist?access_token=ACCESS_TOKEN";

    //取消拉黑微信用户 POST
    private final static String cancel_black_user_api = "https://api.weixin.qq.com/cgi-bin/tags/members/batchunblacklist?access_token=ACCESS_TOKEN";

    /**
     * 获取用户信息
     * @param openId openid微信对应公众号唯一标识
     * @return
     */
    public ApiResult getUser(String openId){
        String accessToken = AccessToken.getAccessTokenStr(appId, appSecret);
        String result = HttpKit.sendGET(user_api.replace("ACCESS_TOKEN", accessToken).replace("OPENID", openId));
        return new ApiResult(result);
    }

    /**
     * 公众号给用户进行备注
     * @param jsonStr
     * {
    "openid":"oDF3iY9ffA-hqb2vVvbr7qxf6A0Q",
    "remark":"pangzi"
    }
     * @return
     */
    public ApiResult updateRemark(String jsonStr){
        String accessToken = AccessToken.getAccessTokenStr(appId, appSecret);
        String result = HttpKit.sendPost(update_remark_api.replace("ACCESS_TOKEN", accessToken), jsonStr);
        return new ApiResult(result);
    }

    /**
     * 批量获取用户信息
     * @param jsonStr
     * {
    "user_list": [
    {
    "openid": "otvxTs4dckWG7imySrJd6jSi0CWE",
    "lang": "zh_CN"
    },
    {
    "openid": "otvxTs_JZ6SEiP0imdhpi50fuSZg",
    "lang": "zh_CN"
    }
    ]
    }
     * @return
     */
    public ApiResult batchGetUsers(String jsonStr){
        String accessToken = AccessToken.getAccessTokenStr(appId, appSecret);
        String result = HttpKit.sendPost(batch_user_api.replace("ACCESS_TOKEN", accessToken), jsonStr);
        return new ApiResult(result);
    }

    /**
     * 拉取微信用户列表
     * @param nextOpenId 第一个拉取的OPENID，不填默认从头开始拉取
     * @return
     */
    public ApiResult getUserList(String nextOpenId){
        String accessToken = AccessToken.getAccessTokenStr(appId, appSecret);
        String url = user_list_api.replace("ACCESS_TOKEN", accessToken);
        if(nextOpenId == null)
            url = url.replace("&next_openid=NEXT_OPENID", "");
        String result = HttpKit.sendGET(url);
        return new ApiResult(result);
    }


    /**
     * 拉取微信用户黑名单列表
     * @param jsonStr
     * {
    "begin_openid":"OPENID1"
    }
     * @return
     */
    public ApiResult getBlackUserList(String jsonStr){
        String accessToken = AccessToken.getAccessTokenStr(appId, appSecret);
        String result = HttpKit.sendPost(black_user_list_api.replace("ACCESS_TOKEN", accessToken), jsonStr);
        return new ApiResult(result);
    }

    /**
     * 批量拉黑用户
     * @param jsonStr
     * {
    "opened_list":["OPENID1”,” OPENID2”]
    }
     * @return
     */
    public ApiResult batchBlackUser(String jsonStr){
        String accessToken = AccessToken.getAccessTokenStr(appId, appSecret);
        String result = HttpKit.sendPost(batch_black_user_api.replace("ACCESS_TOKEN", accessToken), jsonStr);
        return new ApiResult(result);
    }

    /**
     *  取消拉黑微信用户
     * @param jsonStr
     * {
    "opened_list":["OPENID1”,” OPENID2”]
    }
     * @return
     */
    public ApiResult cancelBlackUser(String jsonStr){
        String accessToken = AccessToken.getAccessTokenStr(appId, appSecret);
        String result = HttpKit.sendPost(cancel_black_user_api.replace("ACCESS_TOKEN", accessToken), jsonStr);
        return new ApiResult(result);
    }
}
